var pageSize = 10;
var pageNum = 1;
var page;
var laypage;

$(function () {
    $("#menuId").val(sessionStorage.menuId);
    layui.use(['laydate', 'element', 'laypage', 'layer'], function () {
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        element = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层

        //以上模块根据需要引入
    });
    setTimeout(function () {
        modifyInviter();
    }, 200);

});

function modifyInviter() {
    var userId = sessionStorage.userId;
    $("#userId").val(userId);
    $("#invitePersonAll").html("无数据");
    $("#phone").val("");
    $("#page").html("");
    $("#dataTotal").html("准备就绪...");
}

function queryUser() {
    var phone = $("#phone").val();
    if (phone == "") {
        alert("输入框不能为空");
        return false;
    }
    var menuId = $("#menuId").val();
    var params = {
        "type": "query",
        "menuId": menuId,
        "pageSize": pageSize,
        "pageNum": pageNum,
        "queryKey": phone
    };

    $.ajax({
        type: "post",
        url: "/user/findUserListByNiceNameOrPhone",
        data: params,
        dataType: "json",
        success: function (data) {
            if (data.code == 1 && data.code != undefined) {
                page = data.page;
                inviteLoad(data.list);
            } else {
                $("#invitePersonAll").html("无数据")
            }
            laypage({
                cont: 'page'
                , pages: page.pages
                , first: 1
                , curr: pageNum
                , last: page.pages
                , prev: '<em><</em>'
                , next: '<em>></em>'
                , jump: function (obj, first) {
                    pageNum = obj.curr;
                    if (!first) {
                        queryUser();
                    }
                }
            });
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log("error=" + errorThrown);
        }
    });
}

function inviteLoad(data) {
    var invitePersonAllHtml = "";

    for (var i = 0; i < data.length; i++) {
        invitePersonAllHtml += teacherTr(data[i]);
    }
    $("#invitePersonAll").html(invitePersonAllHtml);
    $("#dataTotal").html("共搜索出：" + page.total + "人");
}

function teacherTr(data) {
    var html = "";
    var state = "正常";
    if (data.state == -1) {
        state = "封停";
    }
    if (1 == data.state) {
        state = "正常";
    }

    var sex;
    switch (data.sex) {
        case 0:
            sex = '男';
            break;
        case 1:
            sex = '女';
            break;
        case 2:
            sex = '不限';
            break;
        default:
            sex = "";
    }

    var role = "";
    if (data.role == 'USER') {
        role = '普通用户';
    }
    if (data.role == 'SUPER') {
        role = '超管';
    }
    if (data.role == 'CHUANG') {
        role = '创客';
    }
    if (data.role == 'HEHUOREN') {
        role = '合伙人';
    }

    html += '<tr class="odd">' +
        /*'<td class="center  sorting_1">'+
        '</td>'+*/
        '<td class=" ">' + data.id + '</td>' +
        '<td class=" ">' + (data.nickName != null ? data.nickName : "") + '</td>' +
        '<td class=" ">' + sex + '</td>' +
        '<td class=" ">' + (!!data.birthday ? new Date(data.birthday).Format("yyyy-MM-dd") : "") + '</td>' +
        '<td class=" ">' + (data.phone != null ? data.phone : "") + '</td>' +
        '<td class=" ">' + role + '</td>' +
        '<td class=" " style="color:red">' + state + '</td>' +
        '<td class=" ">' + (!!data.createTime ? new Date(data.createTime).Format("yyyy-MM-dd hh:mm:ss") : "") + '</td>' +
        '<td class=" ">' +
        '<label><input type="radio" name="optionsRadios" value="' + data.id + '">就选TA</label>' +
        '</td>' +
        '</tr>';
    return html;
}

function exchange4Click() {
    var userId = $("#userId").val();
    var userId4 = $('input[name="optionsRadios"]:checked').val();
    if (userId == null || userId4 == null || userId == "" || userId4 == "") {
        alert("提交失败,数据异常");
        return false;
    }
    if (userId != null && userId4 != null) {
        var menuId = $("#menuId").val();
        var params = {
            "type": "query",
            "menuId": menuId,
            "userId": userId4,
            "newUserId": userId
        };

        $.ajax({
            type: "post",
            url: "/user/update/invite",
            data: params,
            dataType: "json",
            success: function (data) {
                if (data != null) {
                    if (data.status != 1) {
                        alert(data.msg);
                        return false;
                    } else {
                        alert("修改成功");
                    }
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log("error=" + errorThrown);
            }
        });
    }
}

